<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Vip;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class VipController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Vip(), function (Grid $grid) {
            $grid->model()->orderBy('day');

            $grid->column('id');
            $grid->column('day');
            $grid->column('price')->display(function ($price) {
                return $price ? bcmul($price, 0.01, 2) : 0;
            });
            $grid->column('desc');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Vip(), function (Show $show) {
            $show->field('id');
            $show->field('day');
            $show->field('price');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Vip(), function (Form $form) {
            $form->display('id');
            $form->number('day')->required();
            $form->number('price', '价格')->required()->help('单位:分');
            $form->text('desc')->help('例如:月卡');
            $form->display('created_at');
            $form->display('updated_at');

            $form->submitted(function (Form $form) {
                if ($form->day <= 0) {
                    $form->responseValidationMessages('day', '天数必须大于0');
                }
                if ($form->price <= 0) {
                    $form->responseValidationMessages('price', '价格必须大于0');
                }
            });
        });
    }
}
